package jdbc0830;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 完成用户注册功能
 * 程序启动后，要求在控制台上输入他的用户名，密码，等信息
 */
public class RegDemo {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection= DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "ROOT"
        );
        System.out.println("与数据库成功链接");

        Statement statement=connection.createStatement();
        /*
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入用户名");
        String name=scan.nextLine();
        System.out.println("请输入密码");
        String password=scan.nextLine();
        System.out.println("请输入昵称");
        String nickname=scan.nextLine();
        System.out.println("请输入年龄");
        int  age=Integer.parseInt(scan.nextLine());

         */
        UserInfo userInfo=InputUtil.getInputObject(new UserInfo(),"注册用户","注册");
        /*
        String sql="INSERT INTO userinfo (username,password,nickname,age) " +
                "VALUES('"+name+"','"+password+"','"+nickname+"','"+age+"')";
        int num=statement.executeUpdate(sql);
        if(num>0){
            System.out.println("注册完毕");
        }

         */
        String sql="INSERT INTO userinfo (username,password,nickname,age) " +
                "VALUES('"+userInfo.getUsername()+"','"+userInfo.getPassword()+"','"+userInfo.getNickname()+"','"+userInfo.getAge()+"')";
        int num=statement.executeUpdate(sql);
        if(num>0){
            System.out.println("注册完毕");
        }
    }
}
